package com.neuedu.mp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neuedu.mp.entity.SysUser;
import com.neuedu.mp.entity.SysUserRole;
import com.neuedu.mp.mapper.SysUserMapper;
import com.neuedu.mp.mapper.SysUserRoleMapper;
import com.neuedu.mp.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author 金山老师
 * @since 2023-03-15
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Autowired
    SysUserRoleMapper sysUserRoleMapper;




    @Override
    @Transactional
    public boolean grant(Long userId, Long[] roleIds) {


        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id"  , userId );
        sysUserRoleMapper.delete(queryWrapper);



        int count = 0;
        for (Long roleId : roleIds) {
            SysUserRole entity = new SysUserRole();
            entity.setUserId(userId);
            entity.setRoleId(roleId);
            count += sysUserRoleMapper.insert(entity);
        }

        return count == roleIds.length ;
    }
}
